package myqueue;

//单链表实现队列
class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }
}

public class MyQueue {
    private ListNode head;
    private ListNode last;

    //入队
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if (head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            last = node;
        }
    }

    //出队
    public int poll() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int n = head.val;
        head = head.next;
        return n;
    }

    public boolean isEmpty() {
        return head == null;
    }

    public int peek() {
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return head.val;
    }
}
